package com.itheima.service.impl;

import com.itheima.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * ClassName: SendMailServiceImpl
 * Description:
 * Date: 2023/03/14 AM 12:43:42
 * <p>
 * project: springboot
 * package: com.itheima.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class SendMailServiceImpl2 implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送人
     */
    private String from = "1085844536@qq.com";

    /**
     * 收件人
     */
    private String to = "1085844536@qq.com";

    /**
     * 主题
     */
    private String subject = "测试邮件";

    /**
     * 正文
     */
    private String context = "<img src='https://bkimg.cdn.bcebos.com/pic/0e2442a7d933c895d14379402c4264f082025aafaaf4'/><a href='https://www.itcast.com'>点开有惊喜</a>";

    /**
     * 发送邮件
     */
    @Override
    public void sendMail() {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from + "(小甜甜)");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context, true);

            // 添加附件
            File f1 = new File("D:\\Developer\\WGJ\\Java_WorkSpace\\springboot\\springboot_23_mail\\target\\springboot_23_mail-0.0.1-SNAPSHOT.jar");
            File f2 = new File("D:\\Developer\\WGJ\\Java_WorkSpace\\springboot\\springboot_23_mail\\src\\main\\resources\\test.jpg");

            helper.addAttachment(f1.getName(), f1);
            helper.addAttachment("最靠谱的培训机构.png", f2);

            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
